﻿using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Selenium
{
    class Email
    {
        string to;

        public string To
        {
            get { return to; }
            set { to = value; }
        }
        string body;

        public string Body
        {
            get { return body; }
            set { body = value; }
        }
        string subject;

        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }

        public void openOwa(string userName, string password, WebDriver driver)
        {
            driver.goToUrl("https://win-m4n3psr54be.contoso.local/owa");

            WebElement.SendKeys(driver.findElement(By.Id("username")), userName);

            WebElement.SendKeys(driver.findElement(By.Id("password")), password);

            WebElement.Click(driver.findElement(By.XPath("//*[@type=\"submit\"]")));

            driver.HomePage = driver.getFirstWindow();
        }

        public void sendEmail(Email email, WebDriver driver)
        {
            WebElement.Click(driver.findElement(By.Id("newmsgc")));

            driver.switchToWindow(driver.getLastWindow());

            WebElement.SendKeys(driver.findElement(By.Id("divTo")), email.To);

            WebElement.SendKeys(driver.findElement(By.Id("txtSubj")), email.Subject);

            driver.switchToFrame("ifBdy");

            WebElement.SendKeys(driver.findElement(By.XPath("/html/body")), email.Body);

            driver.switchToWindow(driver.getLastWindow());

            WebElement.Click(driver.findElement(By.Id("divToolbarButtonsend")));
        }

        public void deleteLastEmail(WebDriver driver)
        {
            driver.switchToWindow(driver.HomePage);

            WebElement.Click(driver.findElement(By.Id("divTrNdHl"), 2));

            WebElement.SendKeys(driver.findElement(By.Id("divViewport")), Keys.Home);

            WebElement.Click(driver.findElement(By.XPath("//*[@id=\"delete\"]/span")));

            WebElement.Click(driver.findElement(By.XPath("//*[@fldrnm=\"Deleted Items\"]")));
        }

        public void restoreLastMessage(WebDriver driver)
        {
            driver.switchToWindow(driver.HomePage);

            WebElement.Click(driver.findElement(By.XPath("//*[@fldrnm=\"Deleted Items\"]")));
            
            WebElement.Click(driver.findElement(By.Id("divToolbarButtonmove"), 1));

            WebElement.Click(driver.findElement(By.Id("divMoveToFolder")));

            WebElement.Click(driver.findElement(By.Id("fLgAAAACNHoufrugMSZs4CjboaHSGAQCWpTanfhWRTqv4U5yg3TkkAAAA/4q+AAAB"), 1));

            WebElement.Click(driver.findElement(By.Id("btn1")));
        }

        public Email getLastMessageInfo(WebDriver driver)
        {
            Email email = new Email();

            driver.switchToWindow(driver.HomePage);

            WebElement.Click(driver.findElement(By.Id("divTrNdHl"), 6));

            WebElement.SendKeys(driver.findElement(By.Id("divViewport")), Keys.Home);

            email.Subject= driver.findElement(By.Id("divConvTopic")).Text;

            email.Body=driver.findElement(By.XPath("//*[@id=\"divBdy\"]/div/div/div/font/span")).Text;

            email.To=driver.findElement(By.Id("spnR")).Text;

            return email;
        }
    }
}
